package com.skyworth.ui.api;

import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.ui.api.i.ISkyDialogView;
import com.skyworth.ui.define.SkyTextSize;
import com.skyworth.util.SkyScreenParams;
import com.skyworth.ui.api.SkyDialogView.InnerButton;

/**
 * Created by lu on 2018/1/10.
 */

public class SkyDialogViewV2 extends SkyBaseDialogView implements ISkyDialogView {
    public static abstract class DefaultContentView extends RelativeLayout {
        protected static final int LEFT_RIGHT_PADDING = 59;
        protected Context mContext;

        public DefaultContentView(@NonNull Context context) {
            super(context);
            initView(context);
        }

        public DefaultContentView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initView(context);
        }

        public DefaultContentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView(context);
        }

        private void initView(Context context) {
            mContext = context;
            setBackground(SkyDialogView.getContentBG(context));
        }

        protected final int getResolutionValue(int value) {
            return SkyScreenParams.getInstence(mContext).getResolutionValue(value);
        }

        protected final int getTextDpiValue(int value) {
            return SkyScreenParams.getInstence(mContext).getTextDpiValue(value);
        }

        protected final void setTopBottomPadding(int topPadding, int bottomPadding) {
            setPadding(0, getResolutionValue(topPadding), 0, getResolutionValue(bottomPadding));
            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            if (layoutParams != null) {
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                setLayoutParams(layoutParams);
            }
        }

        public abstract void update();
    }

    public static class ContentViewType1 extends DefaultContentView implements OnClickListener, OnKeyListener {
        public interface ContentViewEventListener {
            boolean onKeyEvent(int keyCode, KeyEvent event);

            void onButton1ClickEvent();

            void onButton2ClickEvent();
        }

        public static final int WIDTH = 666;
        public static final int HEIGHT = ViewGroup.LayoutParams.WRAP_CONTENT;
        public static final int BUTTON1 = 1, BUTTON2 = 2;
        private String tips1Text, tips2Text;
        private TextView tips1, tips2;
        private String button1Text, button2Text;
        private InnerButton button1, button2;
        private int focusButton = BUTTON1;//1:button1  2: button2
        private ContentViewEventListener listener;

        public ContentViewType1(@NonNull Context context) {
            super(context);
            initView();
        }

        public ContentViewType1(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initView();
        }

        public ContentViewType1(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView();
        }

        private void initView() {
            {
                tips1 = new TextView(mContext);
                tips1.setId(1);
                tips1.setTextSize(getTextDpiValue(SkyTextSize.t_4));
                tips1.setTextColor(mContext.getResources().getColor(R.color.a_3));
                tips1.setLineSpacing(getResolutionValue(13), 1);
            }
            {
                tips2 = new TextView(mContext);
                tips2.setId(2);
                tips2.setTextSize(getTextDpiValue(SkyTextSize.t_2));
                tips2.setTextColor(mContext.getResources().getColor(R.color.a_2));
            }
            {
                button1 = new InnerButton(mContext);
                button1.setId(11);
                button1.setOnClickListener(this);
                button1.setOnKeyListener(this);
            }
            {
                button2 = new InnerButton(mContext);
                button2.setId(12);
                button2.setOnClickListener(this);
                button1.setOnKeyListener(this);
            }
        }

        public ContentViewType1 withTips(String tips1Text, String tips2Text) {
            this.tips1Text = tips1Text;
            this.tips2Text = tips2Text;
            return this;
        }

        public ContentViewType1 withButtons(String button1Text, String button2Text) {
            this.button1Text = button1Text;
            this.button2Text = button2Text;
            return this;
        }

        public ContentViewType1 withDefaultFocusOn(int focusButton) {
            this.focusButton = focusButton;
            return this;
        }

        public ContentViewType1 withContentViewEventListener(ContentViewEventListener listener) {
            this.listener = listener;
            return this;
        }

        public void updateTips(String tips1Text, String tips2Text) {
            this.tips1Text = tips1Text;
            this.tips2Text = tips2Text;
            post(new Runnable() {
                @Override
                public void run() {
                    updateViewLayout(false);
                }
            });
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                if (v == button1)
                    listener.onButton1ClickEvent();
                else if (v == button2)
                    listener.onButton2ClickEvent();
            }
        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP)
                return false;
            if (listener != null) {
                boolean ret = listener.onKeyEvent(keyCode, event);
                if (ret)
                    return ret;
            }
            return false;
        }

        private int drawButtons(int topMargin, int aboveOfViewID, boolean twoBtn, boolean initFocus) {
            topMargin -= InnerButton.OFFSET;
            if (!twoBtn) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(InnerButton.calculateSideLength(mContext, 300), InnerButton.calculateSideLength(mContext, 70));
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                layoutParams.addRule(RelativeLayout.BELOW, aboveOfViewID);
                layoutParams.leftMargin = InnerButton.calculateMargin(mContext, 183);
                layoutParams.topMargin = getResolutionValue(topMargin);
                if (button1.getParent() == null) {
                    addView(button1, layoutParams);
                    if (initFocus)
                        button1.post(new Runnable() {
                            @Override
                            public void run() {
                                button1.requestFocus();
                            }
                        });
                }
            } else {
                {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(InnerButton.calculateSideLength(mContext, 240), InnerButton.calculateSideLength(mContext, 70));
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.addRule(RelativeLayout.BELOW, aboveOfViewID);
                    layoutParams.leftMargin = InnerButton.calculateMargin(mContext, 68);
                    layoutParams.topMargin = getResolutionValue(topMargin);
                    if (button1.getParent() == null)
                        addView(button1, layoutParams);
                }
                {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(InnerButton.calculateSideLength(mContext, 240), InnerButton.calculateSideLength(mContext, 70));
                    layoutParams.addRule(RelativeLayout.RIGHT_OF, button1.getId());
                    layoutParams.addRule(RelativeLayout.BELOW, aboveOfViewID);
                    layoutParams.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(50 - 2 * InnerButton.OFFSET);
                    layoutParams.topMargin = getResolutionValue(topMargin);
                    if (button2.getParent() == null)
                        addView(button2, layoutParams);
                }
                if (initFocus)
                    button2.post(new Runnable() {
                        @Override
                        public void run() {
                            if (focusButton == 1)
                                button1.requestFocus();
                            else
                                button2.requestFocus();
                        }
                    });
            }
            return InnerButton.OFFSET;
        }

        private void updateViewLayout(final boolean initFocus) {
            boolean twoTips = false;
            {
                if (true) {
                    tips1.setText(tips1Text);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(getResolutionValue(548), ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//                    layoutParams.leftMargin = getResolutionValue(59);
                    if (tips1.getParent() == null)
                        addView(tips1, layoutParams);
                }

                if (!TextUtils.isEmpty(tips2Text)) {
                    tips2.setText(tips2Text);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(getResolutionValue(548), ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.addRule(RelativeLayout.BELOW, tips1.getId());
//                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                    layoutParams.topMargin = getResolutionValue(30);
//                    layoutParams.leftMargin = getResolutionValue(59);
                    if (tips2.getParent() == null)
                        addView(tips2, layoutParams);
                    twoTips = true;
                }
            }
            boolean twoBtn = false;
            if (!TextUtils.isEmpty(button1Text)) {
                button1.setText(button1Text);
            }
            if (!TextUtils.isEmpty(button2Text)) {
                button2.setText(button2Text);
                twoBtn = true;
            }
            final boolean _twoBtn = twoBtn;
            if (!twoTips) {
                tips1.post(new Runnable() {
                    @Override
                    public void run() {
                        int lineCount = tips1.getLineCount();
                        if (lineCount == 1)
                            tips1.setGravity(Gravity.CENTER_HORIZONTAL);
                        else
                            tips1.setGravity(Gravity.LEFT);
                        int buttonTopMargin;
                        int topMargin, bottomMargin;
                        switch (lineCount) {
                            case 1: {
                                buttonTopMargin = getResolutionValue(88);
                                topMargin = 128;
                                break;
                            }
                            case 2: {
                                buttonTopMargin = getResolutionValue(68);
                                topMargin = 92;
                                break;
                            }
                            case 3: {
                                buttonTopMargin = getResolutionValue(47);
                                topMargin = 79;
                                break;
                            }
                            default: {
                                buttonTopMargin = getResolutionValue(50);
                                topMargin = 90;
                                break;
                            }
                        }
                        bottomMargin = topMargin - drawButtons(buttonTopMargin, tips1.getId(), _twoBtn, initFocus);
                        setTopBottomPadding(topMargin, bottomMargin);
                    }
                });
            } else {
                tips1.post(new Runnable() {
                    @Override
                    public void run() {
                        final int lineCount = tips1.getLineCount();
                        if (lineCount == 1)
                            tips1.setGravity(Gravity.CENTER_HORIZONTAL);
                        else
                            tips1.setGravity(Gravity.LEFT);
                        tips2.post(new Runnable() {
                            @Override
                            public void run() {
                                int buttonTopMargin;
                                int topMargin, bottomMargin;
                                switch (lineCount) {
                                    case 2: {
                                        buttonTopMargin = getResolutionValue(61);
                                        topMargin = 79;
                                        break;
                                    }
                                    case 3: {
                                        buttonTopMargin = getResolutionValue(47);
                                        topMargin = 64;
                                        break;
                                    }
                                    default: {
                                        buttonTopMargin = getResolutionValue(50);
                                        topMargin = 70;
                                        break;
                                    }
                                }
                                bottomMargin = topMargin - drawButtons(buttonTopMargin, tips2.getId(), _twoBtn, initFocus);
                                setTopBottomPadding(topMargin, bottomMargin);
                            }
                        });
                    }
                });
            }
        }

        @Override
        public void update() {
            removeAllViews();
            updateViewLayout(true);
        }
    }

    public static final SkyDialogViewV2 withContentViewType1(Context context, String button1Text, String button2Text, String tips1Text, String tips2Text, ContentViewType1.ContentViewEventListener listener) {
        SkyDialogViewV2 view = new SkyDialogViewV2(context);
        view.withContentView(new ContentViewType1(context)
                        .withButtons(button1Text, button2Text)
                        .withTips(tips1Text, tips2Text)
                        .withContentViewEventListener(listener),
                ContentViewType1.WIDTH,
                ContentViewType1.HEIGHT);
        return view;
    }

    private DefaultContentView contentView;
    protected Context mContext;

    public SkyDialogViewV2(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public SkyDialogViewV2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SkyDialogViewV2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public synchronized SkyDialogViewV2 withContentView(DefaultContentView contentView, int width, int height) {
        this.contentView = contentView;
        int _width = width, _height = height;
        if (width >= 0)
            _width = getResolutionValue(width);
        if (height >= 0)
            _height = getResolutionValue(height);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(_width, _height);
        layoutParams.gravity = Gravity.CENTER;
        addView(contentView, layoutParams);
        return this;
    }

    public synchronized SkyDialogViewV2 clearContentView() {
        this.contentView = null;
        removeAllViews();
        return this;
    }

    private void initView(Context context) {
        mContext = context;
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        setBackgroundColor(Color.argb(179, 0, 0, 0));
    }

    @Override
    public void setTipsString(String tips_1, String tips_2) {

    }

    @Override
    public void setBtnFocus(int index) {

    }

    @Override
    public void setBtnString(String btn_1, String btn_2) {

    }

    @Override
    public void show() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (SkyDialogViewV2.this) {
                    if (contentView != null) {
                        contentView.setVisibility(View.VISIBLE);
                        contentView.update();
                        invalidate();
                    }
                }
            }
        };
        if (isInEditMode())
            runnable.run();
        else
            post(runnable);
    }

    @Override
    public void dismiss() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                removeAllViews();
            }
        };
        if (Looper.myLooper() == Looper.getMainLooper())
            runnable.run();
        else
            post(runnable);
    }

    private int getResolutionValue(int value) {
        return SkyScreenParams.getInstence(mContext).getResolutionValue(value);
    }
}
